
package com.sskj.jkzx.service.impl;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sskj.jkzx.domain.entity.EngQueryParam;
import com.sskj.jkzx.domain.entity.eng.EngBidInfo;
import com.sskj.jkzx.domain.entity.eng.EngEmpInfo;
import com.sskj.jkzx.domain.entity.eng.FileInfo;
import com.sskj.utils.HttpHelp;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping(value = "**/sskj/zb")
@Api(value = "招标信息", description = "招标信息")
public class ZbInfoServiceImpl {

	protected final Log logger = LogFactory.getLog(ZbInfoServiceImpl.class);

	@Value("${sskj.cmisServer}")
	private String cmisServer;

	/***
	 * 招标信息
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/zbgc", method = RequestMethod.GET)
	@ApiOperation(value = "标段工程信息查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "sgzbId", value = "施工招标", paramType = "form", dataType = "string"),
			@ApiImplicitParam(name = "sgzbId", value = "监理招标", paramType = "form", dataType = "string") })
	public List<EngBidInfo> queryEngBidInfo(@ApiIgnore EngQueryParam engQueryParam) {

		List<EngBidInfo> results = new ArrayList<EngBidInfo>();

		try {
			String sgs = HttpHelp
					.sendGet(cmisServer + "/seam/resource/v1/jkzx/eng/ztbinfo/" + engQueryParam.getSgzbId(), null);
			if (sgs != null) {
				results.addAll(JSON.parseArray(sgs, EngBidInfo.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		try {
			String jls = HttpHelp
					.sendGet(cmisServer + "/seam/resource/v1/jkzx/eng/ztbinfo/" + engQueryParam.getJlzbId(), null);
			if (jls != null) {
				results.addAll(JSON.parseArray(jls, EngBidInfo.class));
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

	/***
	 * 招标代理
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/zbdlry", method = RequestMethod.GET)
	@ApiOperation(value = "招标代理人员信息查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "xmId", value = "关键字", paramType = "form", dataType = "string") })
	public List<EngEmpInfo> queryEngDEmpInfo(@ApiIgnore EngQueryParam engQueryParam) {

		List<EngEmpInfo> results = new ArrayList<EngEmpInfo>();

		try {
			String str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/jkzx/eng/zbdlry/" + engQueryParam.getXmId(),
					null);

			results = JSON.parseArray(str, EngEmpInfo.class);
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

	/***
	 * 中标机构
	 * 
	 * @return
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/zbjgry", method = RequestMethod.GET)
	@ApiOperation(value = "中标机构人员信息查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "xmId", value = "关键字", paramType = "form", dataType = "string") })
	public List<EngEmpInfo> queryEngZEmpInfo(@ApiIgnore EngQueryParam engQueryParam) {

		List<EngEmpInfo> results = new ArrayList<EngEmpInfo>();

		try {
			String str = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/jkzx/eng/zbjgry/" + engQueryParam.getXmId(),
					null);

			results = JSON.parseArray(str, EngEmpInfo.class);
		} catch (Exception e) {
			logger.info(e.getMessage());
		}

		logger.info(JSONArray.toJSONString(results));

		return results;
	}

	/***
	 * 招标文件查询
	 * 
	 * @return
	 * @throws IOException
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/zbwj", method = RequestMethod.GET)
	@ApiOperation(value = "招标文件查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "docId", value = "ID", paramType = "query", dataType = "string") })
	public void queryZbwjPDF(String docId, HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		FileInfo result = new FileInfo();

		try {
			String ss = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/file/jkzx/zbwj/" + docId, null);

			if (ss != null) {
				result = JSONObject.parseObject(ss, FileInfo.class);
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		} 

		if (result.getData() != null) {
			response.addHeader("content-type", "application/pdf");
			response.getOutputStream().write(result.getData());
		}
	}

	/***
	 * 中标通知书查询
	 * 
	 * @return
	 * @throws IOException
	 */
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/zbtzs", method = RequestMethod.GET)
	@ApiOperation(value = "中标通知书查询", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "docId", value = "ID", paramType = "query", dataType = "string") })
	public void queryZbtzsPDF(String docId, HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		FileInfo result = new FileInfo();

		try {
			String ss = HttpHelp.sendGet(cmisServer + "/seam/resource/v1/file/jkzx/zbtzs/" + docId, null);

			if (ss != null) {
				result = JSONObject.parseObject(ss, FileInfo.class);
			}
		} catch (Exception e) {
			logger.info(e.getMessage());
		}
 
		if (result.getData() != null) {
			response.addHeader("content-type", "application/pdf");
			response.getOutputStream().write(result.getData());
		}
	}

}
